<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 16:21
 */


namespace App\Transformers;

use App\Models\UserBase;
use App\Models\UserFriends;
use Illuminate\Support\Str;
use League\Fractal\TransformerAbstract;

class UserFriendTransformer extends TransformerAbstract
{

    /**
     * @param UserFriends $userFriends
     * @return array
     */
    public function transform(UserFriends $userFriends)
    {
        //  返回用户好友信息
        return [
            'id' => $userFriends->id,
            'user_account' => $userFriends->user_account,
            'f_user_account' => $userFriends->f_user_account,
            'f_user_name' => $userFriends->user->name,
            'memo_name' => $userFriends->memo_name,
            'f_identity' => $this->getPic($userFriends->user),

            'created_at' => $userFriends->created_at->toDateTimeString(),
        ];
    }


    public function getPic(UserBase $user)
    {


        $data = [];
        if ($user->type == 1) {
            $data['pic'] = 'http://www.365tang.cn/upload/face/user_man.png';
            $data['is_doctor'] = 0;
            $data['rong_account'] = 'patient_' . $user->account;
            if ($user->patient) {

                if ($user->patient->PIC) {
                    if (!Str::startsWith($user->patient->PIC, 'http')) {
                        $data['pic'] = 'http://www.365tang.cn/upload/face/' . $user->patient->PIC;
                    } else {
                        $data['pic'] = $user->patient->PIC;
                    }
                }
            }

        } else {
            $data['is_doctor'] = 1;
            $data['rong_account'] = 'doctor_' . $user->account;

            if ($user->sex == 1) {
                $data['pic'] = 'http://www.365tang.cn/upload/public/doctor_female.png';
            } else {
                $data['pic'] = 'http://www.365tang.cn/upload/public/doctor_male.png';
            }
            if ($user->detail) {
                if ($user->detail->PIC) {
                    if (!Str::startsWith($user->detail->PIC, 'http')) {
                        $data['pic'] = 'http://www.365tang.cn/upload/' . $user->detail->PIC;
                    } else {
                        $data['pic'] = $user->detail->PIC;
                    }
                }
            }

        }
        return $data;
    }


}
